package chapter14;

import java.lang.reflect.InvocationTargetException;

/**
 * @author : DuJiabao
 * @Date : 2021/2/24 21:37
 * @Project : thinking-in-java
 * @File : null.java
 * @Desc :
 */
interface HasBatteries {
}

interface Waterproof {
}

interface Shoots {
}

class Toy {
  public Toy() {
  }

  public Toy(int i) {
  }
}

class FancyToy extends Toy implements HasBatteries, Waterproof, Shoots {
  public FancyToy() {
    super(1);
  }
}

public class ToyTest {
  /**
   * 输出Class的一些信息
   * @param cc
   */
  static void printInfo(Class<?> cc) {
    System.out.println("Class name: " + cc.getName());  // 全限定类名
    System.out.println("Is interface? [" + cc.isInterface() + "]");  // 判断是否为接口
    System.out.println("Simple name: " + cc.getSimpleName());  // 不包括包名的类名
    System.out.println("Canonical name: " + cc.getCanonicalName());  // 全限定类名
  }

  public static void main(String[] args) {
    Class<?> c = null;
    try {
      c = Class.forName("chapter14.FancyToy");
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
      System.exit(1);
    }
    printInfo(c);
    for (Class<?> face : c.getInterfaces()) {
      printInfo(face);
    }
    Class<?> up = c.getSuperclass();
    Object obj = null;
    try {
      obj = up.getDeclaredConstructor().newInstance();
    } catch (InstantiationException e) {
      System.out.println("Cannot instantiate");
      System.exit(1);
    } catch (IllegalAccessException e) {
      System.out.println("Cannot access");
      System.exit(1);
    } catch (InvocationTargetException e) {
      System.out.println("Cannot Invocation Target");
      System.exit(1);
    } catch (NoSuchMethodException e) {
      System.out.println("No Such Method");
      System.exit(1);
    }
    printInfo(obj.getClass());
  }
}
